//
//  AddressEditVC.m
//  Ufan
//
//  Created by yangqingluo on 15/12/9.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "AddressEditVC.h"
#import "AddressEditCell.h"

@interface AddressEditVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *showTableView;
@property (nonatomic, strong) UFAddressInfo *address;
@property (nonatomic, strong) UIButton *setDefaultButton;

@end

@implementation AddressEditVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.showTableView reloadData];
}

- (instancetype)initWithAddress:(UFAddressInfo *)info{
    self = [super init];
    
    if (self) {
        if (info) {
            self.address = info;
        }
        else{
            self.address = [UFAddressInfo new];
            self.address.def = YES;
        }
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupBarButtonItem];
}

- (void)setupBarButtonItem{
    self.title = @"编辑地址";
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 44)];
    [backButton setImage:[UIImage imageNamed:@"nav_back.png"] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(12, 0, 12, 48)];
    backButton.tag = 988;
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:backItem];
    
    UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(self.navigationController.navigationBar.width - 64, 0, 64, 44)];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
    saveButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    saveButton.tag = 989;
    [saveButton addTarget:self action:@selector(rightBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithCustomView:saveButton];
    [self.navigationItem setRightBarButtonItem:saveItem];
}

-(void)goBack{
    [self dismissKeyBoard];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)dismissKeyBoard{
    [self.view endEditing:YES];
}


- (void)rightBarButtonClick{
    [self dismissKeyBoard];
    
    if (!self.address.deliveryName) {
        [self showHint:@"请填写收货人姓名"];
    }
    else if (self.address.deliveryPhone.length == 0) {
        [self showHint:@"请填写手机号"];
    }
    else if (self.address.deliveryPhone.length != kPhoneNumberLength) {
        [self showHint:@"请填写正确的手机号"];
    }
    else if (!self.address.detailAddress) {
        [self showHint:@"请填写收货的详细地址"];
    }
    else if (!self.address.name) {
        [self showHint:@"请填写地址备注，比如家，公司等"];
    }
    else{
        self.address.def = self.setDefaultButton.selected;
        [self showHudInView:self.view hint:@"保存中..."];
        [QKHttpManager updateAddress:self.address completion:^(NSObject *object, NSError *error){
            [self hideHud];
            
            if (!error) {
                if ([object isKindOfClass:[NSDictionary class]]) {
                    if ([((NSDictionary *)object)[@"code"] intValue] == QKHTTP_SUCCESS) {
                        if (self.address.def) {
                            [AppPublic shareInstanceApp].addressSelected = self.address;
                        }
                        [self.navigationController popViewControllerAnimated:YES];
                        return;
                    }


                    [self showHint:[(NSDictionary *)object objectForKey:@"msg"]];
                    return;
                }
            }
            [self showHint:@"网络出错"];
        }];
        
    }
}

- (void)setDefaultButtonClick:(UIButton *)button{
    button.selected = !button.selected;
}

#pragma getter
- (UITableView *)showTableView{
    if (!_showTableView) {
        _showTableView= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStylePlain];
        _showTableView.multipleTouchEnabled = YES;
        _showTableView.dataSource = self;
        _showTableView.delegate = self;
        _showTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_showTableView setBackgroundColor:[UIColor clearColor]];
        
        [self.view addSubview:_showTableView];
        
        UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _showTableView.width, 60)];
        _showTableView.tableFooterView = footerView;
        
        self.setDefaultButton.center = CGPointMake(15 + 0.5 * self.setDefaultButton.width, 0.5 * footerView.height);
        [footerView addSubview:self.setDefaultButton];
    }
    
    return _showTableView;
}

- (UIButton *)setDefaultButton{
    if (!_setDefaultButton) {
        _setDefaultButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _setDefaultButton.frame = CGRectMake(0, 0, 140, 30);
        _setDefaultButton.selected = self.address.def;
        _setDefaultButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_setDefaultButton setImage:[UIImage imageNamed:@"public_check_unselected"] forState:UIControlStateNormal];
        [_setDefaultButton setImage:[UIImage imageNamed:@"public_check_selected"] forState:UIControlStateSelected];
        [_setDefaultButton setTitle:@"    设为默认地址" forState:UIControlStateNormal];
        [_setDefaultButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_setDefaultButton addTarget:self action:@selector(setDefaultButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _setDefaultButton;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"AddressEditCell";
    AddressEditCell *cell = (AddressEditCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[AddressEditCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.type = (int)indexPath.row;
    cell.address = self.address;
    
    return cell;
}


#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [AddressEditCell tableView:tableView heightForType:(int)indexPath.row];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

@end
